/*
 * @Author: whr2349
 * @Date: 2021-07-05 10:11:23
 * @LastEditors: whr2349
 * @LastEditTime: 2021-07-08 16:00:23
 * @Description: file content
 * @FilePath: \back\app\service\organization.js
 */
'use strict';

const Service = require('egg').Service;

class OrganizationService extends Service {
    /**
     * 组织列表
     * @param {当前页} currentPage 
     * @param {每页数量} pageSize 
     * @param {排序obj} sort 
     * @returns 
     */
    async index(currentPage, pageSize, sort = { createdTime: -1 }) {
        const { ctx } = this
        const result = await ctx.model.Organization.find({})
            .skip((currentPage - 1) * pageSize).limit(parseInt(pageSize)).sort(sort)
        return result
    }
    async treeIndex(currentPage, pageSize, sort = { createdTime: -1 }) {

        const { ctx } = this

        let result = await ctx.helper.deepTree({
            model: "Organization",
            pName: "superiorKey",
            pValue: "",
            name: "key"
        })

        return result
    }
    /**
     * 根据id创建或修改
     * @param {obj,包含_id则更改，没有则创建新doc} param 
     * @returns 
     */
    async findByIdAndUpdate(param) {
        const { ctx } = this
        const ObjectId = ctx.app.mongoose.Types.ObjectId
        const result = await ctx.model.Organization.findByIdAndUpdate(param._id ? param._id : new ObjectId, param, {
            new: true,
            upsert: true,
            setDefaultsOnInsert: true
        })
        return result
    }

}

module.exports = OrganizationService;
